package models

import (
	"cttu/models/db"
	"cttu/models/structs"
	"fmt"

	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/sqlite"
	"log"
)
//
//const (
//	maxIdleConns = 20
//	maxOpenConns = 20
//)

// InitDB 数据库连接
func InitDB() {
	//source := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local",
	//	"root", "root", "39.105.47.232", "3306", "laravel-shop")
	//dbCli, err := gorm.Open("mysql", source)
	dbCli, err := gorm.Open("sqlite3", "./database/cttu.db")
	if err != nil {
		fmt.Println("初始化sqlite3错误:", err)
		return
	}
	//dbCli.DB().SetMaxIdleConns(maxIdleConns)
	//dbCli.DB().SetMaxOpenConns(maxOpenConns)
	//dbCli.Model(true)
	//dbCli.SingularTable(true)
	AutoMigrate(dbCli)
	db.DB = dbCli
	log.Println("init db ok.")
	return
}

// AutoMigrate 表迁移
func AutoMigrate(db *gorm.DB) {
	log.Println(db.Debug().AutoMigrate(

		&structs.User{},

	).Error)
}
